package com.service.impl;

import com.mapper.ProductsMapper;
import com.pojo.Products;
import com.pojo.Result;
import com.service.ProductsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ProductsServiceImpl implements ProductsService {

    @Autowired
    private ProductsMapper productsMapper;

    @Override
    public Result<Void> createProduct(Products product) {
        try {
            // 设置创建时间和更新时间
            LocalDateTime now = LocalDateTime.now();
            product.setCreatedAt(now);
            product.setUpdatedAt(now);

            // 设置默认可用状态
            if (product.getAvailable() == null) {
                product.setAvailable(true);
            }

            int result = productsMapper.insert(product);
            if (result > 0) {
                return Result.success();
            } else {
                return Result.error("创建产品失败");
            }
        } catch (Exception e) {
            return Result.error(500, "服务器内部错误: " + e.getMessage());
        }
    }

    @Override
    public Result<Void> updateProduct(Products product) {
        try {
            // 设置更新时间
            product.setUpdatedAt(LocalDateTime.now());

            int result = productsMapper.update(product);
            if (result > 0) {
                return Result.success();
            } else {
                return Result.error("更新产品失败，可能ID不存在");
            }
        } catch (Exception e) {
            return Result.error(500, "服务器内部错误: " + e.getMessage());
        }
    }

    @Override
    public Result<Void> deleteProduct(Integer id) {
        try {
            int result = productsMapper.delete(id);
            if (result > 0) {
                return Result.success();
            } else {
                return Result.error("删除产品失败，可能ID不存在");
            }
        } catch (Exception e) {
            return Result.error(500, "服务器内部错误: " + e.getMessage());
        }
    }

    @Override
    public Result<Products> getProductById(Integer id) {
        try {
            Products product = productsMapper.selectById(id);
            if (product != null) {
                return Result.success(product);
            } else {
                return Result.error("产品不存在");
            }
        } catch (Exception e) {
            return Result.error(500, "服务器内部错误: " + e.getMessage());
        }
    }

    @Override
    public Result<List<Products>> getAllProducts() {
        try {
            List<Products> products = productsMapper.selectAll();
            return Result.success(products);
        } catch (Exception e) {
            return Result.error(500, "服务器内部错误: " + e.getMessage());
        }
    }

    @Override
    public Result<List<Products>> getAvailableProducts() {
        try {
            List<Products> products = productsMapper.selectAvailable();
            return Result.success(products);
        } catch (Exception e) {
            return Result.error(500, "服务器内部错误: " + e.getMessage());
        }
    }
}